# (1) 计算某段文本中原音字母出现的次数

def cal_num(text):
    count = 0
    for char in text:
        if char.lower() in "aeiou":
            count += 1

    return count


data = input("请输入一段文本:")
c = cal_num(data)
print("元音字母出现次数：", c)


# (2) 计算初始本金为1万，年利率为0.0325的情况下，需要多少年才能将本金和利息翻倍，即本金和利息的总和达到原来的两倍。


def cal_year(base, back, rate=0.0325):
    # base = 10000
    # rate = 0.0325
    total = base
    year = 0
    while total < back:
        total = total + total * rate
        # print(total)
        year += 1

    return year


print(cal_year(10000,20000))
print(cal_year(10000,100000,0.03))


# (3) 编写一个程序，生成斐波那契数列的第20个数字（斐波那契数列是指从0和1开始，后面的每一项都是前两项的和）
# 0 1 1 2 3 5
def get_fib(n):
    pre = 0
    current = 1
    next_num = 1
    for _ in range(n):
        next_num = pre + current
        # print("next:", next_num)
        pre = current
        current = next_num

    return next_num


print(get_fib(10))


# (4) 打印n*n矩阵
def get_matrix(n):
    # n = 5
    for r in range(1, n + 1):
        for c in range(1, n + 1):
            print(f"{r + c}\t", end="")
        print()

get_matrix(5)
get_matrix(7)
get_matrix(10)


#  (5) 重写 max min函数

num = [23, 2, 5, 66, 76, 12, 88, 23, 65]


def my_max(numbers):
    max_value = numbers[0]
    for i in numbers:
        if i > max_value:
            max_value = i

    return max_value

print(my_max(num))
print(max(num))


# (6) 获取字典中最大值和对应键

def get_max_val(my_dict):
    max_val = 0
    max_val_key = None
    for key, val in my_dict.items():
        if val > max_val:
            max_val = val
            max_val_key = key

    return (max_val_key, max_val)


data = {'A': 10, 'B': 5, 'C': 35, 'D': 20}

print(get_max_val(data))

# (7) 查询列表元素重复次数

def cal_val_num(l):
    count_dict = {}

    for i in l:
        if i in count_dict:
            # count_dict[i] +=1
            count_dict[i] = count_dict[i] + 1
        else:
            count_dict[i] = 1

    return count_dict


my_list = [1, 2, 3, 2, 1, 3, 4, 5, 2, 1]

print(cal_val_num(my_list))
